package com.dexilaonian.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dexilaonian.R;
import com.dexilaonian.adpter.RecoveryAdapter;
import com.dexilaonian.fragment.AllOrderFragment;
import com.dexilaonian.fragment.OverFragment;
import com.dexilaonian.fragment.UnfinishedFragment;
import com.dexilaonian.ui.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class OrderActivity extends BaseActivity implements View.OnClickListener {
    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.base_topText)
    TextView baseTopText;
    @Bind(R.id.tv_order_all)
    TextView tvOrderAll;
    @Bind(R.id.iv_order1)
    View ivOrder1;
    @Bind(R.id.tv_unfinished)
    TextView tvUnfinished;
    @Bind(R.id.iv_order2)
    View ivOrder2;
    @Bind(R.id.tv_order_over)
    TextView tvOrderOver;
    @Bind(R.id.iv_order3)
    View ivOrder3;
    @Bind(R.id.order_ViewPager)
    ViewPager orderViewPager;
    private List<Fragment> orderList = new ArrayList<>();
    private RecoveryAdapter orderAdapter;

    @Override
    public int getLayoutID() {
        return R.layout.activity_order;
    }

    @Override
    public void initView() {
    }
    @Override
    protected void initData() {
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        baseTopText.setText("我的订单");
        init();
    }

    private void init() {
        AllOrderFragment allOrderFragment = new AllOrderFragment();
        UnfinishedFragment unfinishedFragment = new UnfinishedFragment();
        OverFragment overFragment = new OverFragment();
        orderList.add(allOrderFragment);
        orderList.add(unfinishedFragment);
        orderList.add(overFragment);
        orderAdapter = new RecoveryAdapter(OrderActivity.this.getSupportFragmentManager(), orderList);
        orderViewPager.setAdapter(orderAdapter);
        tvOrderAll.setOnClickListener(this);
        tvUnfinished.setOnClickListener(this);
        tvOrderOver.setOnClickListener(this);
        orderViewPager.setCurrentItem(0);
        orderViewPager.setOffscreenPageLimit(3);
        orderViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            @Override
            public void onPageSelected(int position) {
                chanceColorText(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void chanceColorText(int position) {
        if (position == 0) {
            tvOrderAll.setTextColor(Color.parseColor("#3aaffc"));
            tvUnfinished.setTextColor(Color.parseColor("#333333"));
            tvOrderOver.setTextColor(Color.parseColor("#333333"));
            ivOrder1.setBackground(getResources().getDrawable(R.drawable.fuwuxian));
            ivOrder2.setBackground(null);
            ivOrder3.setBackground(null);

        } else if (position == 1) {
            tvUnfinished.setTextColor(Color.parseColor("#3aaffc"));
            tvOrderAll.setTextColor(Color.parseColor("#333333"));
            tvOrderOver.setTextColor(Color.parseColor("#333333"));
            ivOrder2.setBackground(getResources().getDrawable(R.drawable.fuwuxian));
            ivOrder1.setBackground(null);
            ivOrder3.setBackground(null);
        } else if (position == 2) {
            tvOrderOver.setTextColor(Color.parseColor("#3aaffc"));
            tvUnfinished.setTextColor(Color.parseColor("#333333"));
            tvOrderAll.setTextColor(Color.parseColor("#333333"));
            ivOrder3.setBackground(getResources().getDrawable(R.drawable.fuwuxian));
            ivOrder2.setBackground(null);
            ivOrder1.setBackground(null);
        }
    }

    @OnClick({R.id.base_topText,R.id.back, R.id.tv_order_all, R.id.tv_unfinished, R.id.tv_order_over})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
            case R.id.base_topText:
                finish();
                break;
            case R.id.tv_order_all:
                orderViewPager.setCurrentItem(0);
                tvOrderAll.setTextColor(Color.parseColor("#3aaffc"));
                tvUnfinished.setTextColor(Color.parseColor("#333333"));
                tvOrderOver.setTextColor(Color.parseColor("#333333"));
                ivOrder1.setBackground(getResources().getDrawable(R.drawable.fuwuxian));
                ivOrder2.setBackground(null);
                ivOrder3.setBackground(null);
                break;
            case R.id.tv_unfinished:
                orderViewPager.setCurrentItem(1);
                tvUnfinished.setTextColor(Color.parseColor("#3aaffc"));
                tvOrderAll.setTextColor(Color.parseColor("#333333"));
                tvOrderOver.setTextColor(Color.parseColor("#333333"));
                ivOrder2.setBackground(getResources().getDrawable(R.drawable.fuwuxian));
                ivOrder1.setBackground(null);
                ivOrder3.setBackground(null);
                break;
            case R.id.tv_order_over:
                orderViewPager.setCurrentItem(2);
                tvOrderOver.setTextColor(Color.parseColor("#3aaffc"));
                tvUnfinished.setTextColor(Color.parseColor("#333333"));
                tvOrderAll.setTextColor(Color.parseColor("#333333"));
                ivOrder3.setBackground(getResources().getDrawable(R.drawable.fuwuxian));
                ivOrder2.setBackground(null);
                ivOrder1.setBackground(null);
                break;
        }
    }
}
